<?php

/*
 * qbbcom 记录获取 @author @version 0.1
 */

abstract class esb_rpc_requestpublic_request {
	
	// API访问地址
	const SIP_URL = '';
	
	// const SIP_APPKEY = '12295977';
	// 供货商编码
	const SIP_APPKEY = '11002200';
	// API 版本
	const API_VERSION = '1.0';
	// SIGN 算法
	const SIGN_METHOD = 'md5';
	// 返回格式
	const RETURN_FORMAT = 'json';
	var $timeout = 10;
	
	/**
	 * 调用gome API接口
	 *
	 * @param Array $stock        	
	 */
	protected function apiRequest($stock, $msg_id) {
		
		if (! is_array ( $stock ) || empty($stock)) {
			kernel::log ( "stock為空" );
		$obj  =	(object) array('returnCode' => 'S', 'returnInfo' => 'stock為空');
			return $obj;
		}
		
		$xmlstock = '';
		
		foreach ( $stock as $k => $v ) {
			$xmlstock = $xmlstock . "<stock><goods_sn>{$v['sku']}</goods_sn><qty>{$v['quantity']}</qty></stock>";
		}
		$xmlstock = "<stocks>" . $xmlstock . "</stocks>";
		
		$signString = $msg_id . self::SIP_APPKEY . "gome";
		$signMethod = self::SIGN_METHOD;
		$sign = $signMethod($signString);
		
		$xml = "<root><msg_id>$msg_id</msg_id><key>$sign</key>
		<provider_no>" . self::SIP_APPKEY . "</provider_no>$xmlstock</root>";
		kernel::log("gomeapi xml:".$xml);
		// $xml = urlencode($xml);
		try {
			
			    $client = new SoapClient("http://supplier.gome.com.cn/ws/gome/sp/IPlatStockServiceImpl?wsdl");
			$parm = array (
					'arg0' => $xml 
			);
			
			$result = $client->updateStock ( $parm );
			$result = $result->return;
			kernel::log("gomeapi return:".json_encode($result));
		
		} catch ( SoapFault $fault ) {		
			$result = "Error: " . $fault->faultcode . ", string: " . $fault->faultstring;
			kernel::log("gomeapi return:".$result);
		}
		
		return $result;
	}
}